package com.frame.ar.util;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.sf.json.JSONObject;

import com.frame.ar.hander.ValueHander;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Record;

/**
 * @author LRC
 * @createDate 2015年5月27日
 * @description map对象类型数据工具类
 */
public class MapUtil {

	public static boolean copyToMap(JSONObject data,Map<String,Object> map, String[] args) {
		if (map == null || data == null)
			return false;
		if(args==null){
			Set<String> keys = data.keySet();
			for(String key : keys){
				map.put(key, data.get(key));
			}
			return true;
		}
		try {
			String arg;
			String name;
			String type;
			String name2;
			Object val;
			for (int i = 0; i < args.length; i++) {
				arg = args[i];
				String[] ns = arg.split(":");
				name = ns[0];
				switch (ns.length) {
				case 1:
					map.put(name, data.get(name));
					break;
				case 2://拥有类型装换 需要完善
					type=ns[1];
					val = ValueHander.parseVal(data.get(name), type);
					map.put(name, val);
					break;
				case 3://从其他别名中获取参数
					type=ns[1];
					name2=ns[2];
					val = ValueHander.parseVal(data.get(name), type);
					map.put(name2, val);
					break;
				default:
					break;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}

	public static boolean copyToMap(Controller c,Map<String,Object> map, String[] args) {
		if (map == null || c == null || args == null)
			return false;
		try {
			String arg;
			String name;
			String type;
			String name2;
			Object val;
			for (int i = 0; i < args.length; i++) {
				arg = args[i];
				String[] ns = arg.split(":");
				name = ns[1];
				switch (ns.length) {
				case 1:
					map.put(name, c.getPara(name));
					break;
				case 2://拥有类型装换 需要完善
					type=ns[1];
					val = ValueHander.parseVal(c.getPara(name), type);
					map.put(name, val);
					break;
				case 3://从其他别名中获取参数
					type=ns[1];
					name2=ns[2];
					val = ValueHander.parseVal(c.getPara(name), type);
					map.put(name2, val);
					break;
				default:
					break;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return true;
	}

	/*******************************************
	 * 功能描述：从jfinal查询到的列表提取mpa list
	 *******************************************
	 * @createDate 2015-9-12 by LRC
	 * @param list
	 * @return 
	 * @updateRecord 格式：时间，作者，修改说明
	 *******************************************
	 * 
	 */
	public static List<Map<String,Object>> getMapListFromRecord(List<Record> list){
		if(list==null)return null;
		List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();
		for(Record r : list){
			data.add(r.getColumns());
		}
		return data;
	}
	
	/**
	 * 功能描述：从controller对象中赋值数据
	 * @createDate 2015年10月24日 by LRC
	 * @param c
	 * @param args
	 * @return 
	 * @updateRecord 格式：时间，作者，修改说明
	 **
	 * 
	 */
	public static Map<String,Object> create(Controller c,String[] args) {
		if( c == null )
			return null;
		Map<String,Object> map = new HashMap<String,Object>();
		if(args==null){
			Enumeration<String> names = c.getParaNames();
			while (names.hasMoreElements()) {
				String name = (String) names.nextElement();
				map.put(name, c.getPara(name));
			}
			return map;
		}
		try {
			String arg;
			String name;
			String type;
			String name2;
			Object val;
			for (int i = 0; i < args.length; i++) {
				arg = args[i];
				String[] ns = arg.split(":");
				name = ns[1];
				switch (ns.length) {
				case 1:
					map.put(name, c.getPara(name));
					break;
				case 2://拥有类型装换 需要完善
					type=ns[1];
					val = ValueHander.parseVal(c.getPara(name), type);
					map.put(name, val);
					break;
				case 3://从其他别名中获取参数
					type=ns[1];
					name2=ns[2];
					val = ValueHander.parseVal(c.getPara(name), type);
					map.put(name2, val);
					break;
				default:
					break;
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return map;
	}
	
}
